package cn.cdnsu.auth.service.impl;

import cn.cdnsu.auth.annotation.NsuPermission;
import cn.cdnsu.auth.domain.Permission;
import cn.cdnsu.auth.mapper.PermissionMapper;
import cn.cdnsu.auth.service.IPermissionScanService;
import cn.cdnsu.base.enums.RequestTypeEnum;
import cn.cdnsu.base.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@Service
public class PermissionScanServiceImpl implements IPermissionScanService {

    @Value("${basepackge.base-pack}")
    private String pack;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void scan() {
        System.out.println(pack);

        permissionMapper.deleteAll();
        //拿到所有的包
        List<Class> allClassName = ClassUtils.getAllClassName(pack);
        System.out.println(allClassName);

        for (Class clazz: allClassName){
            //获取“类“上是否有自定义注解，如果类上没注解，往下拿方法
            NsuPermission annotation = (NsuPermission)clazz.getAnnotation(NsuPermission.class);
           //获取类上的RequestMapping
            RequestMapping annotation2 = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (Objects.isNull(annotation)){
                continue;
            }
            String simpleName = clazz.getSimpleName();

            String[] value = annotation2.value();
            Permission parent = new Permission();
            parent.setName(annotation.name());
            parent.setDescs(annotation.descs());
            parent.setSn(simpleName);
            parent.setUrl(annotation2.value()[0]);
            permissionMapper.insert(parent);


            Method[] methods = clazz.getMethods();
            for (Method method:methods){
                //判断方法是否有自定义注解
                NsuPermission annotation1 = method.getAnnotation(NsuPermission.class);
                if (Objects.isNull(annotation1)){
                    continue;
                }


                //取name 取descs 取url 取sn 取parent
                //封装对象

                Permission permission = new Permission();
                permission.setName(annotation1.name());
                permission.setDescs(annotation1.descs());
                permission.setSn(simpleName+":"+method.getName());
                permission.setUrl(parent.getUrl()+getMethodUrl(method));
                permission.setParent(parent);

                System.out.println(simpleName+":"+method.getName());

                //入库

                permissionMapper.insert(permission);
            }

        }

    }

    public String getMethodUrl(Method method){
        String methodUrl = "";
        // 取枚举中定义的所有的请求类型
        RequestTypeEnum[] requestTypeEnums = RequestTypeEnum.values();
        for (RequestTypeEnum typeEnum : requestTypeEnums) {
            // 根据枚举中的不同的请求类型的class获取该类型对应的注解对象
            Annotation annotation = method.getAnnotation(typeEnum.getRequestType());
            // 如果该请求类型注解不存在就跳过
            if(Objects.isNull(annotation)){
                continue;
            }
            try {
                // 如果该请求类型注解存在,就获取它里面的value方法
                Class<? extends Annotation> aClass = annotation.annotationType();
                Method annotationMethod = aClass.getMethod("value");
                // 通过invoke调用该对象的value方法,获取结果
                // 反射执行 方法 方法名.invoke(对象)
                String[] value = (String[]) annotationMethod.invoke(annotation);
                // 如果value不为空且长度大于0,就赋值给methodUrl,并跳出循环
                if(value != null && value.length > 0){
                    methodUrl = value[0];
                    break;
                }
            }catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return methodUrl;

    }
}
